﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microhard.Framework.MVCHelper
{
    public class ScriptSubmitButton : BaseButton
    {
        private string _submitFormID;

        public string SubmitFormID
        {
            set { this._submitFormID = value; }
            get { return this._submitFormID; }
        }

        public ScriptSubmitButton(string key)
        {
            this._assetKey = key;
        }

        public override System.Web.Mvc.MvcHtmlString WriteHtml(System.Web.Mvc.HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            return base.CreateButton(SysConst.OPERATION_ADD, helper, this._id, linkText, true, controllerName, actionName,this._submitFormID);
        }

        public override string WriteJQueryReadyInnerScript()
        {
            StringBuilder script = new StringBuilder();
            script.Append("$('#" + this._id + "').click(function () {");
            script.Append("var form = $('#" + this._submitFormID + "');");
            foreach (var m in base._passToActionValues)
            {
                script.Append("form.append(");
                script.Append("\"");
                script.Append("<input type='hidden' value=\"+" + m.Value + "+\" name='" + m.Key + "'>");
                script.Append("\");");
            }
            script.Append("return true;");
            script.Append("});");
            return script.ToString();
        }
    }
}
